<?php

include "../include/config.php";
#include "../entity/User.php";

$username = $_POST["username"];
$password = md5($_POST["password"]);

$query = "select * from users where user_name='". $username . "' and password='". $password. "'";

#echo $query; exit; 

$user = $DB->QueryRow($query);

# user data
$id             = $user["id"];
$user_name      = $user["user_name"];
$user_avatar    = $user["user_avatar"];  
$password       = $user["password"];
$email          = $user["email"];
$first_name     = $user["first_name"];
$last_name      = $user["last_name"];    
$birthday       = $user["birthday"];
$register_date  = $user["register_date"];    
$register_ip    = $user["register_ip"];  
$active_code    = $user["active_code"];    
$status         = $user["status"];  
$sign           = $user["sign"];    
$group_id       = $user["group_id"];
$login_count    = $user["login_count"];

# user update data 
$visited_date   = time();
$visited_ip     = $_SERVER['REMOTE_ADDR'];

//login sucessfull

if($id && $group_id==1)
{
	#update info login user
	
	$query = "	UPDATE users 
				SET visited_date=NOW(),
				visited_ip='". $visited_ip ."',
				login_count = login_count+1
				WHERE id=$id";
	
	$DB->Update($query);
	
	#Save session by array
		
	$_SESSION["Login"]["id"]		= $id;
	$_SESSION["Login"]["group_id"]	= $group_id;
	$_SESSION["Login"]["user_name"]	= $user_name;
	$_SESSION["Login"]["first_name"]= $first_name;
	$_SESSION["Login"]["last_name"] = $last_name;
	
	#Set session by object
	
	#$oUser = new User($id, $user_name, $user_avatar, $password, $email, $first_name, $last_name,
    #       $birthday, $register_date, $register_ip, 
    #       $visited_date, $visited_ip, $active_code, $status, $sign, $group_id, $login_count);    
           
	#$_SESSION["Login"] = $oUser; 
	
	echo "<script language='javascript'>alert('Login Sucessfull!');</script>";
}
else
	echo "<script language='javascript'>alert('Login fail!');</script>";
	
header("Location: index.php");
?>